package com.huike.pmps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.pmps.dao.CourseCapacityDao;
import com.huike.pmps.dao.POCourseDao;
import com.huike.pmps.dao.PODao;
import com.huike.pmps.model.constants.GlobalConstants;
import com.huike.pmps.model.dto.common.PageInfo;
import com.huike.pmps.model.entity.*;
import com.huike.pmps.model.vo.PageResult;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.service.*;
import com.huike.pmps.utils.PmpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@Service
@Slf4j
public class POCourseServiceImpl implements POCourseService {
    @Autowired
    private PODao poDao;
    @Autowired
    POCourseDao poCourseDao;
    @Autowired
    POService poService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private RegionService regionService;
    @Autowired
    private ProvincesService provincesService;
    @Autowired
    private POCourseSupplierDetailService poCourseSupplierDetailService;
    @Autowired
    private CourseCapacityDao courseCapacityDao;

    @Override
    public Result insertOne(POCourse poCourse) {
        if (poCourse.getPoId() == null)
            return Result.failure("poId为空");
        PO po = poDao.selectById(poCourse.getPoId());
        BeanUtils.copyProperties(po, poCourse, new String[]{"id", "importFlag", "rowEdit", "state", "updateTime", "createTime"});
        Long courseNo = redisTemplate.opsForValue().increment(GlobalConstants.REDIS_HASH_COURSE_NO, 1);
        poCourse.setCourseId(courseNo);
        //设置课程状态-未启动
        poCourse.setCourseState(GlobalConstants.PO_COURSE_STATE_UNSTART);
        poCourse.setCourseScheduleName("未启动");
        poCourse.setUpdateTime(new Date());
        poCourse.setCreateTime(new Date());
        poCourseDao.insert(poCourse);
        //更新po状态
        //poDao.updatePOState(poCourse.getPoId());
        poDao.updatePOStateNew(poCourse.getPoId(),GlobalConstants.PO_STATE_START,GlobalConstants.PO_COURSE_STATE_START);
        return Result.success();
    }

    @Override
    public Result selectByCondition(Member member, PageInfo pageInfo, String po, String customerName, String regionName, String provinceName,String teamName, String courseName, String courseChargeName, String courseState) {
        QueryWrapper<POCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", GlobalConstants.PO_COURSE_STATE_CONFIRMED);
        if (StringUtils.isNotBlank(po)) {
            queryWrapper.like("po", po);
        }
        if (StringUtils.isNotBlank(customerName)) {
            queryWrapper.like("customer_name", customerName);
        }
        if (StringUtils.isNotBlank(regionName)) {
            queryWrapper.like("region_name", regionName);
        }
        if (StringUtils.isNotBlank(provinceName)) {
            queryWrapper.like("province_name", provinceName);
        }
        if (StringUtils.isNotBlank(teamName)) {
            queryWrapper.like("team_name", teamName);
        }
        if (StringUtils.isNotBlank(courseChargeName)) {
            queryWrapper.like("course_charge_name", courseChargeName);
        }
        if (StringUtils.isNotBlank(courseName)) {
            queryWrapper.like("course_name", courseName);
        }
        if (StringUtils.isNotBlank(courseState)) {
            queryWrapper.like("course_state", courseState);
        }

        if (member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_ADMIN)) {//管理员
        } else if (member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_REGION)) {//战区司令
            if (member.getRegionId() == null) return Result.failure("您的账号没有配置所属战区，请联系 系统管理员 进行战区配置");
            queryWrapper.eq("region_id", member.getRegionId());
        } else if (member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_PROVINCE) && !member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_COURSE_CHARGE)) {
            //是省负责人，但不是课程编导
            if (member.getProvinceId() == null) return Result.failure("您的账号没有配置所属省区，请联系 系统管理员 进行省区配置");
            queryWrapper.eq("province_id", member.getProvinceId());
        } else if (member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_PROVINCE) && member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_COURSE_CHARGE)) {
            //是省负责人，又是课程编导
            if (member.getProvinceId() == null) return Result.failure("您的账号没有配置所属省区，请联系 系统管理员 进行省区配置");
            queryWrapper.and(wrapper ->
                    wrapper.eq("province_id", member.getProvinceId())
                            .or().eq("course_charge_name", member.getMemberName()));

        } else if (member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_COURSE_CHARGE)) {
            queryWrapper.eq("course_charge_name", member.getMemberName());
        } else {
            if (member.getProvinceId() == null) return Result.failure("您的账号没有配置所属省区，请联系 系统管理员 进行省区配置");
            queryWrapper.eq("province_id", member.getProvinceId());
        }


        queryWrapper.orderByDesc("id");
        Page<POCourse> page = new Page<>(pageInfo.getPage(), pageInfo.getPageSize());
        Page<POCourse> pageList = poCourseDao.selectPage(page, queryWrapper);
        if (pageList.getSize() > 0) {
            PageResult pageResult = PageResult.builder().pageNumber(pageList.getCurrent()).pageSize(pageList.getSize())
                    .total(pageList.getTotal()).rows(pageList.getRecords()).build();
            return Result.success(pageResult);
        } else {
            return Result.success("没有符合条件的记录");
        }
    }

    /**
     * 根据ID更新课程数据
     *
     * @param poCourse
     * @return
     */
    @Override
    public Result updateById(POCourse poCourse,Member member) {

        poCourse.setRowEdit(false);
        poCourse.setAcceptanceReportPath(null);

        //po 课程终版时长不为空
        if (StringUtils.isNotBlank(poCourse.getEndEditionTimeLength())) {
            poCourse.setEndEditionTimeLength2(PmpUtil.timeToMinuteConvert(poCourse.getEndEditionTimeLength()));
        }

        //todo 计算课程进度和课程状态
        String earlyExchangeName = poCourse.getEarlyExchangeName();
        if (StringUtils.isNotBlank(earlyExchangeName)) {
            earlyExchangeName = earlyExchangeName.substring(0, earlyExchangeName.length() - 1);
            Integer earlyExchange = Integer.valueOf(earlyExchangeName);
            //如果是admin用户,那么课程状态直接是输入的状态 不用进行逻辑判断
            if(!member.getMemberName().equalsIgnoreCase("admin")){
                poCourse.setCourseState("进行中");
            }
            if (earlyExchange == 0) {
                poCourse.setCourseScheduleName("未启动");
            } else if (earlyExchange > 0 && earlyExchange < 100) {
                poCourse.setCourseScheduleName("沟通中");
            } else if (earlyExchange == 100) {
                if (poCourse.getShootQuantity() == null || poCourse.getShootQuantity() == 0)
                    poCourse.setCourseScheduleName("待拍摄");

                if (poCourse.getEstimateQuantity() != null && poCourse.getShootQuantity() != null && poCourse.getEstimateQuantity() > poCourse.getShootQuantity())
                    poCourse.setCourseScheduleName("拍摄中");

                if (poCourse.getEstimateQuantity() != null && poCourse.getShootQuantity() != null && poCourse.getEstimateQuantity() != 0 &&
                        (poCourse.getShootQuantity() / poCourse.getEstimateQuantity()) >= 1 &&
                        (poCourse.getFirstEditionQuantity() == null || poCourse.getFirstEditionQuantity() <= 0)
                )
                    poCourse.setCourseScheduleName("待制作");

                if (poCourse.getEstimateQuantity() != null && poCourse.getShootQuantity() != null && poCourse.getEstimateQuantity() != 0 &&
                        (poCourse.getShootQuantity() / poCourse.getEstimateQuantity()) >= 1 &&
                        (poCourse.getEndEditionQuantity() != null && (poCourse.getEndEditionQuantity() / poCourse.getEstimateQuantity()) < 1)
                )
                    poCourse.setCourseScheduleName("制作中");

                if (poCourse.getEndEditionQuantity() != null && poCourse.getEstimateQuantity() != null && poCourse.getContractOutstandingPayment() != null && poCourse.getContractOutstandingPayment().intValue() > 0)
                    poCourse.setCourseScheduleName("待回款");

                if (poCourse.getEndEditionQuantity() != null && poCourse.getEstimateQuantity() != null && poCourse.getContractOutstandingPayment() != null && poCourse.getContractOutstandingPayment().intValue() <= 0) {
                    poCourse.setCourseScheduleName("已完结");
                    //如果是admin用户,那么课程状态直接是输入的状态 不用进行逻辑判断
                    if(!member.getMemberName().equalsIgnoreCase("admin")){
                        poCourse.setCourseState("已完结");
                    }
                }

            }
        }

        //todo 计算课程完成度
        Double degreeOfCompletion = 0d;
        if (StringUtils.isNotBlank(earlyExchangeName)) {
            earlyExchangeName = earlyExchangeName.substring(0, earlyExchangeName.length() - 1);
            Integer earlyExchange = Integer.valueOf(earlyExchangeName);
            degreeOfCompletion = earlyExchange * 0.01;
        }
        if (poCourse.getIsTrailer() != null && (poCourse.getIsTrailer() == 0 || poCourse.getIsTrailer() == 2)) {
            degreeOfCompletion += 0.2;
        }
        if (poCourse.getSampleConfirm() != null && poCourse.getSampleConfirm() == 1) {
            degreeOfCompletion += 0.1;
        }
        if (poCourse.getEstimateQuantity() != null && poCourse.getEstimateQuantity() != 0) {
            if (poCourse.getShootQuantity() != null && poCourse.getShootQuantity() != 0) {
                if ((poCourse.getShootQuantity() / poCourse.getEstimateQuantity()) < 0.2) {
                    degreeOfCompletion += (poCourse.getShootQuantity() / poCourse.getEstimateQuantity());
                } else {
                    degreeOfCompletion += 0.2;
                }
            }
            if (poCourse.getFirstEditionQuantity() != null && poCourse.getFirstEditionQuantity() != 0) {
                if ((poCourse.getFirstEditionQuantity() / poCourse.getEstimateQuantity()) < 0.2) {
                    degreeOfCompletion += (poCourse.getFirstEditionQuantity() / poCourse.getEstimateQuantity());
                } else {
                    degreeOfCompletion += 0.2;
                }
            }

            if (poCourse.getEndEditionQuantity() != null && poCourse.getEndEditionQuantity() != 0) {
                if ((poCourse.getEndEditionQuantity() / poCourse.getEstimateQuantity()) < 0.2) {
                    degreeOfCompletion += (poCourse.getEndEditionQuantity() / poCourse.getEstimateQuantity());
                } else {
                    degreeOfCompletion += 0.2;
                }
            }
        }
        if (degreeOfCompletion != null) {
            degreeOfCompletion = (degreeOfCompletion * 100);
            poCourse.setDegreeOfCompletion(degreeOfCompletion.intValue() + "%");
        }

        //todo 更新课程
        POCourse poCourseOld = poCourseDao.selectById(poCourse.getId());
        /**
         * add by zcw 2021-07-04
         * 当 PO下插入课程或更新 PO下的课程，需要更新po的状态
         * PO状态(1未启动、2进行中、3已交付、4已验收  5已完结 6 已终止)
         * @param poId
         * @return
         */
        //1.设置课程启动时间,第一次修改或者分配供应商的时间
        if(poCourseOld.getStartTime() == null){
            poCourse.setStartTime(new Date());
            poCourse.setCourseState(GlobalConstants.PO_COURSE_STATE_START);
        }

        //2.用户选择已交付 设置交付时间
        if(poCourse.getCourseState() != null && "已交付".equalsIgnoreCase(poCourse.getCourseState())){
            poCourse.setDeliverTime(new Date());
            poCourse.setCourseState(GlobalConstants.PO_COURSE_STATE_DELIVER);
        }

        //3.用户选择已验收 设置完结时间
        if(poCourse.getCourseState() != null && "已完结".equalsIgnoreCase(poCourse.getCourseState())){
            poCourse.setFinishTime(new Date());
            poCourse.setCourseState(GlobalConstants.PO_COURSE_STATE_FINISH);
        }

        //4.用户选择已终止 设置终止时间
        if(poCourse.getCourseState() != null && "已终止".equalsIgnoreCase(poCourse.getCourseState())){
            poCourse.setOverTime(new Date());
            poCourse.setCourseState(GlobalConstants.PO_COURSE_STATE_OVER);
        }

        if (poCourseDao.updateById(poCourse) > 0) {
            //todo 更新po状态
            //poDao.updatePOState(poCourse.getPoId());
            /**
             * add by zcw 2021-07-04
             * 当 PO下插入课程或更新 PO下的课程，需要更新po的状态
             * PO状态(1未启动、2进行中、3已交付、4已验收  5已完结 6 已终止)
             * @param poId
             * @return
             */
            if(poCourse.getCourseState() != null){
                if("已交付".equalsIgnoreCase(poCourse.getCourseState())){
                    poDao.updatePOStateNew(poCourse.getPoId(),GlobalConstants.PO_STATE_DELIVER,GlobalConstants.PO_COURSE_STATE_DELIVER);
                }else if("已完结".equalsIgnoreCase(poCourse.getCourseState())){
                    poDao.updatePOStateNew(poCourse.getPoId(),GlobalConstants.PO_STATE_FINISH,GlobalConstants.PO_COURSE_STATE_FINISH);
                }else if("已终止".equalsIgnoreCase(poCourse.getCourseState())){
                    poDao.updatePOStateNew(poCourse.getPoId(),GlobalConstants.PO_STATE_OVER,GlobalConstants.PO_COURSE_STATE_OVER);
                }
            }

            //更新po已完成和未完成时长
            if (StringUtils.isNotBlank(poCourse.getEndEditionTimeLength())) {
                poDao.updatePOContractFinishedTimeLengthByPoId(poCourse.getPoId());
            }

            Date date = new Date();
            //产能有变化，需要插入新的产能记录到 课程-产能表中
            if (StringUtils.isNotBlank(poCourse.getEndEditionTimeLength()) &&
                    poCourseOld.getEndEditionTimeLength() != poCourse.getEndEditionTimeLength()) {
                CourseCapacity courseCapacity = new CourseCapacity();
                BeanUtils.copyProperties(poCourseOld, courseCapacity, new String[]{"id", "", "createTime"});
                courseCapacity.setPoCourseId(poCourseOld.getId());
                if (poCourseOld.getEndEditionTimeLength2() == null) poCourseOld.setEndEditionTimeLength2(0d);
                courseCapacity.setCapacity(poCourse.getEndEditionTimeLength2() - poCourseOld.getEndEditionTimeLength2());
                courseCapacity.setYear(PmpUtil.getDateInfo(date, GlobalConstants.YEAR));
                courseCapacity.setSeasonOfYear(PmpUtil.getDateInfo(date, GlobalConstants.SEASON));
                courseCapacity.setMonthOfYear(PmpUtil.getDateInfo(date, GlobalConstants.MONTH));
                courseCapacity.setWeekOfYear(PmpUtil.getDateInfo(date, GlobalConstants.WEEK_OF_YEAR));
                courseCapacity.setCreateTime(date);
                //todo 插入产能数据
                courseCapacityDao.insert(courseCapacity);
            }
            return Result.success();
        } else {
            return Result.failure("没有可更新的内容");
        }


    }

    @Override
    public Result deleteById(Integer id) {
        Result result = poCourseSupplierDetailService.selectByPoCourseId(id);
        if (result.getData() != null) return Result.failure("请先删除课程下的供应商服务");
        POCourse poCourse = poCourseDao.selectById(id);
        if (poCourseDao.deleteById(id) > 0) {
            //更新po已完成和未完成时长
            poDao.updatePOContractFinishedTimeLengthByPoId(poCourse.getPoId());
            return Result.success();
        } else {
            return Result.failure("没有可删除的内容");
        }
    }

    @Override
    public Result selectByPoId(Integer poId) {
        QueryWrapper<POCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("po_id", poId);
        List<POCourse> courseList = poCourseDao.selectList(queryWrapper);
        if (courseList.size() > 0) {
            return Result.success(courseList);
        } else {
            return Result.failure("没有需要的数据");
        }
    }

    /**
     * 批量更新PO课程状态
     *
     * @param ids
     * @return
     */
    @Override
    public Result updateStateById(String ids) {
        List<POCourse> list = new ArrayList<>();
        String[] items = ids.split(",");
        for (String id : items) {
            list.add(POCourse.builder().id(Integer.valueOf(id)).state(GlobalConstants.PO_COURSE_STATE_CONFIRMED).build());
        }
        poCourseDao.updateStateById(list);
        return Result.success();
    }

    @Override
    public POCourse selectById(Integer id) {
        return poCourseDao.selectById(id);
    }

    /**
     * 更新 po课程验收单路径， po课程-供应商 验收单路径，结算单验收单路径,入库单验收单路径
     *
     * @param poCourseId
     * @param fileName
     * @param filePath
     * @param updateDate
     * @return
     */
    @Override
    public Result updateAcceptanceReport(Integer poCourseId,String receptTimeLength, String fileName, String filePath, Date updateDate) {
        if(poCourseDao.updateAcceptanceReport(poCourseId, receptTimeLength,fileName, filePath, updateDate)>0){
            //更新PO的状态
            POCourse po = poCourseDao.selectById(poCourseId);
            poDao.updatePOStateNew(po.getPoId(),GlobalConstants.PO_STATE_RECEPT,GlobalConstants.PO_COURSE_STATE_RECEPT);
        };
        return Result.success();
    }

    @Override
    public void exportPOCourse(HttpServletResponse response, Member member, String po, String customerName, String regionName, String provinceName,String teamName, String courseName, String courseChargeName, String courseState) {
        QueryWrapper<POCourse> queryWrapper = new QueryWrapper<>();


        if (member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_ADMIN)) {//管理员
        } else if (member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_REGION)) {//战区司令
            queryWrapper.eq("region_id", member.getRegionId());
        } else if (member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_PROVINCE) && !member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_COURSE_CHARGE)) {
            //是省负责人，但不是课程编导
            queryWrapper.eq("province_id", member.getProvinceId());
        } else if (member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_PROVINCE) && member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_COURSE_CHARGE)) {
            //是省负责人，又是课程编导
            queryWrapper.and(wrapper ->
                    wrapper.eq("province_id", member.getProvinceId())
                            .or().eq("course_charge_name", member.getMemberName()));
        } else if (member.getRolesIds().contains(GlobalConstants.SYSTEM_ROLE_COURSE_CHARGE)) {
            queryWrapper.eq("course_charge_name", member.getMemberName());
        } else {
            queryWrapper.eq("province_id", member.getProvinceId());
        }

        queryWrapper.eq("state", GlobalConstants.PO_COURSE_STATE_CONFIRMED);
        if (StringUtils.isNotBlank(po)) {
            queryWrapper.like("po", po);
        }
        if (StringUtils.isNotBlank(customerName)) {
            queryWrapper.like("customer_name", customerName);
        }
        if (StringUtils.isNotBlank(regionName)) {
            queryWrapper.like("region_name", regionName);
        }
        if (StringUtils.isNotBlank(provinceName)) {
            queryWrapper.like("province_name", provinceName);
        }
        if (StringUtils.isNotBlank(teamName)) {
            queryWrapper.like("team_name", teamName);
        }
        if (StringUtils.isNotBlank(courseChargeName)) {
            queryWrapper.like("course_charge_name", courseChargeName);
        }
        if (StringUtils.isNotBlank(courseName)) {
            queryWrapper.like("course_name", courseName);
        }
        if (StringUtils.isNotBlank(courseState)) {
            queryWrapper.like("course_state", courseState);
        }
        List<POCourse> poCourseList = poCourseDao.selectList(queryWrapper);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("export_excel/po_course_template.xlsx");
        XSSFWorkbook xssfWorkbook = null;
        try {
            xssfWorkbook = new XSSFWorkbook(in);
            //获取第一个工作表sheet
            XSSFSheet sheetAt = xssfWorkbook.getSheetAt(0);
            for (int i = 0; i < poCourseList.size(); i++) {
                POCourse poCourse = poCourseList.get(i);
                XSSFRow row = sheetAt.createRow(i + 1);
                //po
                XSSFCell poCell = row.createCell(0);
                poCell.setCellType(CellType.STRING);
                poCell.setCellValue(poCourse.getPo());

                //客户名称
                XSSFCell customerNameCell = row.createCell(1);
                customerNameCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getCustomerName()))
                    customerNameCell.setCellValue(poCourse.getCustomerName());

                //战区
                XSSFCell regionNameCell = row.createCell(2);
                regionNameCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getRegionName()))
                    regionNameCell.setCellValue(poCourse.getRegionName());

                //省份
                XSSFCell provinceNameCell = row.createCell(3);
                provinceNameCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getProvinceName()))
                    provinceNameCell.setCellValue(poCourse.getProvinceName());

                //客户成功
                XSSFCell teamNameCell = row.createCell(4);
                teamNameCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getTeamName()))
                    teamNameCell.setCellValue(poCourse.getTeamName());

                //课程编导
                XSSFCell courseChargeNameCell = row.createCell(5);
                courseChargeNameCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getCourseChargeName()))
                    courseChargeNameCell.setCellValue(poCourse.getCourseChargeName());

                //课程ID
                XSSFCell courseIdCell = row.createCell(6);
                courseIdCell.setCellType(CellType.STRING);
                if (poCourse.getCourseId() != null)
                    courseIdCell.setCellValue(poCourse.getCourseId());


                //学科门类
                XSSFCell subjectCategoryNameCell = row.createCell(7);
                subjectCategoryNameCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getSubjectCategoryName()))
                    subjectCategoryNameCell.setCellValue(poCourse.getSubjectCategoryName());

                //专业类别
                XSSFCell majorCategoryCell = row.createCell(8);
                majorCategoryCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getMajorCategory()))
                    majorCategoryCell.setCellValue(poCourse.getMajorCategory());

                //课程名称
                XSSFCell courseNameCell = row.createCell(9);
                courseNameCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getCourseName()))
                    courseNameCell.setCellValue(poCourse.getCourseName());

                //责任教师
                XSSFCell teacherCell = row.createCell(10);
                teacherCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getTeacher()))
                    teacherCell.setCellValue(poCourse.getTeacher());

                //责任教师联系方式
                XSSFCell phoneCell = row.createCell(11);
                phoneCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getPhone()))
                    phoneCell.setCellValue(poCourse.getPhone());

                //前期沟通进度
                XSSFCell earlyExchangeNameCell = row.createCell(12);
                earlyExchangeNameCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getEarlyExchangeName()))
                    earlyExchangeNameCell.setCellValue(poCourse.getEarlyExchangeName());

                //课程类别
                XSSFCell courseTypeNameCell = row.createCell(13);
                courseTypeNameCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getCourseTypeName()))
                    courseTypeNameCell.setCellValue(poCourse.getCourseTypeName());

                //拍摄形式
                XSSFCell shootModeNameCell = row.createCell(14);
                shootModeNameCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getShootModeName()))
                    shootModeNameCell.setCellValue(poCourse.getShootModeName());

                //预估数量
                XSSFCell estimateQuantityCell = row.createCell(15);
                estimateQuantityCell.setCellType(CellType.STRING);
                if (poCourse.getEstimateQuantity() != null)
                    estimateQuantityCell.setCellValue(poCourse.getEstimateQuantity());

                //预估时长
                XSSFCell estimateTimeLengthCell = row.createCell(16);
                estimateTimeLengthCell.setCellType(CellType.STRING);
                if (poCourse.getEstimateTimeLength() != null)
                    estimateTimeLengthCell.setCellValue(poCourse.getEstimateTimeLength());

                //样片确认
                XSSFCell sampleConfirmCell = row.createCell(17);
                sampleConfirmCell.setCellType(CellType.STRING);
                if (poCourse.getSampleConfirm() != null)
                    sampleConfirmCell.setCellValue(poCourse.getSampleConfirm() == 1 ? "是" : "否");

                //开始拍摄日期
                XSSFCell shootStartDateCell = row.createCell(18);
                shootStartDateCell.setCellType(CellType.STRING);
                if (poCourse.getShootStartDate() != null)
                    shootStartDateCell.setCellValue(simpleDateFormat.format(poCourse.getShootStartDate()));

                //开始制作日期
                XSSFCell makeStartDateCell = row.createCell(19);
                makeStartDateCell.setCellType(CellType.STRING);
                if (poCourse.getMakeStartDate() != null)
                    makeStartDateCell.setCellValue(simpleDateFormat.format(poCourse.getMakeStartDate()));

                //项目交付日期
                XSSFCell projectDeliveryDateCell = row.createCell(20);
                projectDeliveryDateCell.setCellType(CellType.STRING);
                if (poCourse.getProjectDeliveryDate() != null)
                    projectDeliveryDateCell.setCellValue(simpleDateFormat.format(poCourse.getProjectDeliveryDate()));

                //拍摄数量
                XSSFCell shootQuantityCell = row.createCell(21);
                shootQuantityCell.setCellType(CellType.STRING);
                if (poCourse.getShootQuantity() != null)
                    shootQuantityCell.setCellValue(poCourse.getShootQuantity());

                //初版数量
                XSSFCell firstEditionQuantityCell = row.createCell(22);
                firstEditionQuantityCell.setCellType(CellType.STRING);
                if (poCourse.getFirstEditionQuantity() != null)
                    firstEditionQuantityCell.setCellValue(poCourse.getFirstEditionQuantity());

                //终版数量
                XSSFCell endEditionQuantityCell = row.createCell(23);
                endEditionQuantityCell.setCellType(CellType.STRING);
                if (poCourse.getEndEditionQuantity() != null)
                    endEditionQuantityCell.setCellValue(poCourse.getEndEditionQuantity());

                //终版时长
                XSSFCell endEditionTimeLengthCell = row.createCell(24);
                endEditionTimeLengthCell.setCellType(CellType.STRING);
                if (poCourse.getEndEditionTimeLength() != null)
                    endEditionTimeLengthCell.setCellValue(poCourse.getEndEditionTimeLength());

                //宣传片
                XSSFCell isTrailerCell = row.createCell(25);
                isTrailerCell.setCellType(CellType.STRING);
                if (poCourse.getIsTrailer() != null)
                    isTrailerCell.setCellValue(poCourse.getIsTrailer() == 1 ? "已制作" : "未制作");

                //完成度
                XSSFCell degreeOfCompletionCell = row.createCell(26);
                degreeOfCompletionCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getDegreeOfCompletion()))
                    degreeOfCompletionCell.setCellValue(poCourse.getDegreeOfCompletion());

                //课程合同金额
                XSSFCell contractAmountCell = row.createCell(27);
                contractAmountCell.setCellType(CellType.STRING);
                if (poCourse.getContractAmount() != null)
                    contractAmountCell.setCellValue(poCourse.getContractAmount().toString());

                //已回款
                XSSFCell contractPaymentReceivedCell = row.createCell(28);
                contractPaymentReceivedCell.setCellType(CellType.STRING);
                contractPaymentReceivedCell.setCellValue(poCourse.getContractPaymentReceived() == null ? "" : poCourse.getContractPaymentReceived().toString());

                //未回款
                XSSFCell contractOutstandingPaymentCell = row.createCell(29);
                contractOutstandingPaymentCell.setCellType(CellType.STRING);
                contractOutstandingPaymentCell.setCellValue(poCourse.getContractOutstandingPayment() == null ? "" : poCourse.getContractOutstandingPayment().toString());

                //预估供应总金额
                XSSFCell estimatedGenerationCostCell = row.createCell(30);
                estimatedGenerationCostCell.setCellType(CellType.STRING);
                estimatedGenerationCostCell.setCellValue(poCourse.getEstimatedGenerationCost() == null ? "" : poCourse.getEstimatedGenerationCost().toString());


                //供应商已结款
                XSSFCell supplierPaidAmountCell = row.createCell(31);
                supplierPaidAmountCell.setCellType(CellType.STRING);
                supplierPaidAmountCell.setCellValue(poCourse.getSupplierPaidAmount() == null ? "" : poCourse.getSupplierPaidAmount().toString());

                //未支付供应商金额
                XSSFCell supplierUnpaidAmountCell = row.createCell(32);
                supplierUnpaidAmountCell.setCellType(CellType.STRING);
                supplierUnpaidAmountCell.setCellValue(poCourse.getSupplierUnpaidAmount() == null ? "" : poCourse.getSupplierUnpaidAmount().toString());

                //上线平台
                XSSFCell onlinePlatformCell = row.createCell(33);
                onlinePlatformCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getOnlinePlatform()))
                    onlinePlatformCell.setCellValue(poCourse.getOnlinePlatform());

                //合同交付时间
                XSSFCell contractDeliveryDateCell = row.createCell(34);
                contractDeliveryDateCell.setCellType(CellType.STRING);
                if (poCourse.getContractDeliveryDate() != null)
                    contractDeliveryDateCell.setCellValue(simpleDateFormat.format(poCourse.getContractDeliveryDate()));

                //课程进度
                XSSFCell courseScheduleNameCell = row.createCell(35);
                courseScheduleNameCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getCourseScheduleName()))
                    courseScheduleNameCell.setCellValue(poCourse.getCourseScheduleName());

                //课程状态
                XSSFCell courseStateCell = row.createCell(36);
                courseStateCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getCourseState()))
                    courseStateCell.setCellValue(poCourse.getCourseState());

                //课程进度备注
                XSSFCell courseScheduleRemarkCell = row.createCell(37);
                courseScheduleRemarkCell.setCellType(CellType.STRING);
                if (StringUtils.isNotBlank(poCourse.getCourseScheduleRemark()))
                    courseScheduleRemarkCell.setCellValue(poCourse.getCourseScheduleRemark());

                //创建时间
                XSSFCell createTimeCell = row.createCell(38);
                createTimeCell.setCellType(CellType.STRING);
                if (poCourse.getCreateTime() != null)
                    createTimeCell.setCellValue(simpleDateFormat2.format(poCourse.getCreateTime()));

                //启动时间
                XSSFCell startTimeCell = row.createCell(39);
                startTimeCell.setCellType(CellType.STRING);
                if (poCourse.getStartTime() != null)
                    startTimeCell.setCellValue(simpleDateFormat2.format(poCourse.getStartTime()));

                //交付时间
                XSSFCell deliverTimeCell = row.createCell(40);
                deliverTimeCell.setCellType(CellType.STRING);
                if (poCourse.getDeliverTime() != null)
                    deliverTimeCell.setCellValue(simpleDateFormat2.format(poCourse.getDeliverTime()));

                //验收时间
                XSSFCell receptTimeCell = row.createCell(41);
                receptTimeCell.setCellType(CellType.STRING);
                if (poCourse.getReceptTime() != null)
                    receptTimeCell.setCellValue(simpleDateFormat2.format(poCourse.getReceptTime()));

                //完成时间
                XSSFCell finishTimeCell = row.createCell(42);
                finishTimeCell.setCellType(CellType.STRING);
                if (poCourse.getFinishTime() != null)
                    finishTimeCell.setCellValue(simpleDateFormat2.format(poCourse.getFinishTime()));

                //终止时间
                XSSFCell overTimeCell = row.createCell(43);
                overTimeCell.setCellType(CellType.STRING);
                if (poCourse.getOverTime() != null)
                    overTimeCell.setCellValue(simpleDateFormat2.format(poCourse.getOverTime()));

                //验收时长
                XSSFCell receptTimeLengthCell = row.createCell(44);
                receptTimeLengthCell.setCellType(CellType.STRING);
                if (poCourse.getReceptTimeLength() != null)
                    receptTimeLengthCell.setCellValue(poCourse.getReceptTimeLength());
            }

            String fileName = "po_course" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + ".xlsx";
            response.setContentType("application/octet-stream"); // 响应内容格式
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            xssfWorkbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            xssfWorkbook.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入po_course课程数据
     *
     * @param file
     * @return
     */
    @Override
    public Result insertByImportPoCourse(MultipartFile file) {
        if (file.isEmpty()) {
            return Result.failure("文件为空");
        }
        try {
            //根据路径获取这个操作excel的实例
            XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
            //根据页面index 获取sheet页
            XSSFSheet sheet = wb.getSheetAt(0);
            //获取标题行
            XSSFRow titleRow = sheet.getRow(0);

            String[] titleRowArray = new String[]{"PO单号", "课程编导", "课程ID", "学科门类", "专业类别", "课程名称", "责任教师", "责任教师联系方式", "前期沟通进度", "课程类别", "拍摄形式", "预估数量", "预估时长", "样片确认", "开始拍摄日期", "开始制作日期", "项目交付日期", "拍摄数量", "初版数量", "终版数量", "终版时长", "宣传片", "完成度", "课程合同金额", "已回款", "未回款", "预估供应总金额", "供应商已结款", "供应商未结款", "上线平台", "合同交付时间", "课程进度", "课程状态", "课程进度备注"};

            for (int i = 0; i < titleRowArray.length; i++) {
                if (!titleRowArray[i].equals(titleRow.getCell(i).toString()))
                    return Result.failure("模板标题行已被修改，请勿修改合同模板标题行");
            }
            //日期格式化
            //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            //实体类集合
            List<POCourse> poCourseList = new ArrayList<>();

            XSSFRow row = null;
            //循环sesheet页中数据从第二行开始，第一行是标题
            for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
                //获取每一行数据
                row = sheet.getRow(i);

                POCourse poCourse = new POCourse();

                //PO单号
                XSSFCell poCell = row.getCell(0);
                if (poCell != null && StringUtils.isNotBlank(poCell.toString())) {
                    String poNo = poCell.toString();
                    PO po = poService.selectOne(poNo);
                    if (po == null)
                        return Result.failure("本次导入全部失败，第【" + i + "】条数据的PO单号无法在po列表中找到，请确保PO单号对应的记录在PO管理列表中");
                    poCourse.setPo(poNo);
                    poCourse.setPoId(po.getId());
                    poCourse.setRegionId(po.getRegionId());
                    poCourse.setRegionName(po.getRegionName());
                    poCourse.setProvinceId(po.getProvinceId());
                    poCourse.setProvinceName(po.getProvinceName());
                    poCourse.setCustomerName(po.getCustomerName());
                    poCourse.setPoChargeId(po.getPoChargeId());
                    poCourse.setPoChargeName(po.getPoChargeName());
                } else {
                    return Result.failure("本次导入全部失败，第【" + i + "】条数据的PO单号为空");
                }

            /*
                //客户名称
                XSSFCell customerNameCell = row.getCell(1);
                if (customerNameCell != null)
                    poCourse.setCustomerName(customerNameCell.toString());

                //战区
                XSSFCell regionNameCell = row.getCell(2);
                if (regionNameCell != null) {
                    Region region = regionService.selectOneByRegionName(regionNameCell.toString());
                    if (region != null) {
                        poCourse.setRegionId(region.getId());
                        poCourse.setRegionName(region.getRegion());
                    } else {
                        return Result.failure("本次导入全部失败，第【" + i + "】条数据的战区无法在系统中查找到,请确保表格中战区名字与系统中存在的战区名字保持一致");
                    }
                }
                //省份
                XSSFCell provinceNameCell = row.getCell(3);
                if (provinceNameCell != null) {
                    Provinces provinces = provincesService.selectByProvinceName(provinceNameCell.toString());
                    if (provinces != null) {
                        poCourse.setProvinceId(provinces.getProvinceId());
                        poCourse.setProvinceName(provinces.getProvince());
                    } else {
                        return Result.failure("本次导入全部失败，第【" + i + "】条数据的省份无法在系统中查找到,请确保表格中战区名字与系统中存在的省份名字保持一致");
                    }
                }

                */

                //课程编导
                XSSFCell courseChargeNameCell = row.getCell(1);
                if (courseChargeNameCell != null && StringUtils.isNotBlank(courseChargeNameCell.toString())) {
                    poCourse.setCourseChargeName(courseChargeNameCell.toString());
                }

                //原始课程ID
                XSSFCell courseIdCell = row.getCell(2);
                if (courseIdCell != null && StringUtils.isNotBlank(courseIdCell.toString())) {
                    poCourse.setImportCourseId(Double.valueOf(courseIdCell.toString()).intValue());
                    //生成新的课程ID
                    Long courseNo = redisTemplate.opsForValue().increment(GlobalConstants.REDIS_HASH_COURSE_NO, 1);
                    poCourse.setCourseId(courseNo);
                } else {
                    return Result.failure("本次导入全部失败，第【" + i + "】条数据课程ID为空");
                }


                //学科门类名称
                XSSFCell subjectCategoryNameCell = row.getCell(3);
                if (subjectCategoryNameCell != null && StringUtils.isNotBlank(subjectCategoryNameCell.toString())) {
                    poCourse.setSubjectCategoryName(subjectCategoryNameCell.toString());
                }

                //专业类别
                XSSFCell majorCategoryCell = row.getCell(4);
                if (majorCategoryCell != null && StringUtils.isNotBlank(majorCategoryCell.toString())) {
                    poCourse.setMajorCategory(majorCategoryCell.toString());
                }

                //课程名称
                XSSFCell courseNameCell = row.getCell(5);
                if (courseNameCell != null && StringUtils.isNotBlank(courseNameCell.toString())) {
                    poCourse.setCourseName(courseNameCell.toString());
                }

                //责任教师
                XSSFCell teacherCell = row.getCell(6);
                if (teacherCell != null && StringUtils.isNotBlank(teacherCell.toString())) {
                    poCourse.setTeacher(teacherCell.toString());
                }

                //责任教师联系方式
                XSSFCell phoneCell = row.getCell(7);
                if (phoneCell != null && StringUtils.isNotBlank(phoneCell.toString())) {
                    poCourse.setPhone(phoneCell.toString());
                }

                //前期沟通进度
                XSSFCell earlyExchangeNameCell = row.getCell(8);
                if (earlyExchangeNameCell != null && StringUtils.isNotBlank(earlyExchangeNameCell.toString())) {
                    poCourse.setEarlyExchangeName(earlyExchangeNameCell.toString());
                }

                //课程类别
                XSSFCell courseTypeNameCell = row.getCell(9);
                if (courseTypeNameCell != null && StringUtils.isNotBlank(courseTypeNameCell.toString())) {
                    poCourse.setCourseTypeName(courseTypeNameCell.toString());
                }

                //拍摄形式
                XSSFCell shootModeNameCell = row.getCell(10);
                if (shootModeNameCell != null && StringUtils.isNotBlank(shootModeNameCell.toString())) {
                    poCourse.setShootModeName(shootModeNameCell.toString());
                }

                //拍摄形式
                XSSFCell estimateQuantityCell = row.getCell(11);
                if (estimateQuantityCell != null && StringUtils.isNotBlank(estimateQuantityCell.toString())) {
                    poCourse.setEstimateQuantity(Double.valueOf(estimateQuantityCell.toString()).intValue());
                }

                //预估时长
                XSSFCell estimateTimeLengthCell = row.getCell(12);
                if (estimateTimeLengthCell != null && StringUtils.isNotBlank(estimateTimeLengthCell.toString())) {
                    poCourse.setEstimateTimeLength(Double.valueOf(estimateTimeLengthCell.toString()).intValue());
                }


                //样片是否确认 0否，1是
                XSSFCell sampleConfirmCell = row.getCell(13);
                if (sampleConfirmCell != null && StringUtils.isNotBlank(sampleConfirmCell.toString())) {
                    poCourse.setSampleConfirm(estimateTimeLengthCell.toString() == "是" ? 1 : 0);
                }

                //开始拍摄日期
                XSSFCell shootStartDateCell = row.getCell(14);
                if (shootStartDateCell != null && StringUtils.isNotBlank(shootStartDateCell.toString())) {
                    poCourse.setShootStartDate(shootStartDateCell.getDateCellValue());
                }

                //开始制作日期
                XSSFCell makeStartDateCell = row.getCell(15);
                if (makeStartDateCell != null && StringUtils.isNotBlank(makeStartDateCell.toString())) {
                    poCourse.setMakeStartDate(makeStartDateCell.getDateCellValue());
                }

                //项目交付日期
                XSSFCell projectDeliveryDateCell = row.getCell(16);
                if (projectDeliveryDateCell != null && StringUtils.isNotBlank(projectDeliveryDateCell.toString())) {
                    poCourse.setProjectDeliveryDate(projectDeliveryDateCell.getDateCellValue());
                }

                //拍摄数量
                XSSFCell shootQuantityCell = row.getCell(17);
                if (shootQuantityCell != null && StringUtils.isNotBlank(shootQuantityCell.toString())) {
                    poCourse.setShootQuantity(Double.valueOf(shootQuantityCell.toString()).intValue());
                }

                //初版数量
                XSSFCell firstEditionQuantityCell = row.getCell(18);
                if (firstEditionQuantityCell != null && StringUtils.isNotBlank(firstEditionQuantityCell.toString())) {
                    poCourse.setFirstEditionQuantity(Double.valueOf(firstEditionQuantityCell.toString()).intValue());
                }

                //终版数量
                XSSFCell endEditionQuantityCell = row.getCell(19);
                if (endEditionQuantityCell != null && StringUtils.isNotBlank(endEditionQuantityCell.toString())) {
                    poCourse.setEndEditionQuantity(Double.valueOf(endEditionQuantityCell.toString()).intValue());
                }

                //终版时长
                XSSFCell endEditionTimeLengthCell = row.getCell(20);
                if (endEditionTimeLengthCell != null && StringUtils.isNotBlank(endEditionTimeLengthCell.toString())) {
                    if (endEditionTimeLengthCell.toString().split("[:]").length != 3)
                        return Result.failure("本次导入全部失败，第【" + i + "】条数据的终版时长必须是 HH:MM:ss 格式");
                    poCourse.setEndEditionTimeLength(endEditionTimeLengthCell.toString());
                    poCourse.setEndEditionTimeLength2(PmpUtil.timeToMinuteConvert(endEditionTimeLengthCell.toString()));
                }

                //宣传片(0无,1未制作，2已完成)
                XSSFCell isTrailerCell = row.getCell(21);
                if (isTrailerCell != null && StringUtils.isNotBlank(isTrailerCell.toString())) {
                    poCourse.setIsTrailer(isTrailerCell.toString() == "无" ? 0 : isTrailerCell.toString() == "未制作" ? 1 : 2);
                }

                //完成度
                XSSFCell degreeOfCompletionCell = row.getCell(22);
                if (degreeOfCompletionCell != null && StringUtils.isNotBlank(degreeOfCompletionCell.toString())) {
                    poCourse.setDegreeOfCompletion(degreeOfCompletionCell.toString());
                }

                //课程合同金额
                XSSFCell contractAmountCell = row.getCell(23);
                if (contractAmountCell != null && StringUtils.isNotBlank(contractAmountCell.toString())) {
                    poCourse.setContractAmount(BigDecimal.valueOf(Double.valueOf(contractAmountCell.toString())));
                }

                //已回款
                XSSFCell contractPaymentReceivedCell = row.getCell(24);
                if (contractPaymentReceivedCell != null && StringUtils.isNotBlank(contractPaymentReceivedCell.toString())) {
                    poCourse.setContractPaymentReceived(BigDecimal.valueOf(Double.valueOf(contractPaymentReceivedCell.toString())));
                }

                //未回款
                XSSFCell contractOutstandingPaymentCell = row.getCell(25);
                if (contractOutstandingPaymentCell != null && StringUtils.isNotBlank(contractOutstandingPaymentCell.toString())) {
                    poCourse.setContractOutstandingPayment(BigDecimal.valueOf(Double.valueOf(contractOutstandingPaymentCell.toString())));
                }

                //预估供应总金额
                XSSFCell estimatedGenerationCostCell = row.getCell(26);
                if (estimatedGenerationCostCell != null && StringUtils.isNotBlank(estimatedGenerationCostCell.toString())) {
                    poCourse.setEstimatedGenerationCost(BigDecimal.valueOf(Double.valueOf(estimatedGenerationCostCell.toString())));
                }

                //供应商已结款
                XSSFCell supplierPaidAmountCell = row.getCell(27);
                if (supplierPaidAmountCell != null && StringUtils.isNotBlank(supplierPaidAmountCell.toString())) {
                    poCourse.setSupplierPaidAmount(BigDecimal.valueOf(Double.valueOf(supplierPaidAmountCell.toString())));
                }

                //未支付供应商金额
                XSSFCell supplierUnpaidAmountCell = row.getCell(28);
                if (supplierUnpaidAmountCell != null && StringUtils.isNotBlank(supplierUnpaidAmountCell.toString())) {
                    poCourse.setSupplierUnpaidAmount(BigDecimal.valueOf(Double.valueOf(supplierUnpaidAmountCell.toString())));
                }

                //上线平台
                XSSFCell onlinePlatformCell = row.getCell(29);
                if (onlinePlatformCell != null && StringUtils.isNotBlank(onlinePlatformCell.toString())) {
                    poCourse.setOnlinePlatform(onlinePlatformCell.toString());
                }

                //项目交付日期
                XSSFCell contractDeliveryDateCell = row.getCell(30);
                if (contractDeliveryDateCell != null && StringUtils.isNotBlank(contractDeliveryDateCell.toString())) {
                    poCourse.setContractDeliveryDate(contractDeliveryDateCell.getDateCellValue());
                }

                //课程进度
                XSSFCell courseScheduleNameCell = row.getCell(31);
                if (courseScheduleNameCell != null && StringUtils.isNotBlank(courseScheduleNameCell.toString())) {
                    poCourse.setCourseScheduleName(courseScheduleNameCell.toString());
                }

                //课程状态
                XSSFCell courseStateCell = row.getCell(32);
                if (courseStateCell != null && StringUtils.isNotBlank(courseStateCell.toString())) {
                    poCourse.setCourseTypeName(courseStateCell.toString());
                }

                //课程进度备注
                XSSFCell courseScheduleRemarkCell = row.getCell(33);
                if (courseScheduleRemarkCell != null && StringUtils.isNotBlank(courseScheduleRemarkCell.toString())) {
                    poCourse.setCourseScheduleRemark(courseScheduleRemarkCell.toString());
                }
                //记录状态(0未确认,1已确认)
                poCourse.setState(GlobalConstants.PO_COURSE_STATE_CONFIRMED);
                //导入标记(1非导入,2导入)
                poCourse.setImportFlag(2);
                poCourse.setUpdateTime(new Date());
                poCourse.setCreateTime(new Date());
                poCourseList.add(poCourse);
            }
            if (poCourseList.size() > 0) {
                poCourseDao.insertByImportPoCourse(poCourseList);
                return Result.success();
            } else {
                return Result.failure("表格中没有数据，导入失败");
            }
        } catch (IOException e) {
            log.error(e.getMessage());
            return Result.failure("导入失败，请检查数据格式是否符合模板要求：" + e.getMessage());
        }
    }

    @Override
    public POCourse selectOneByImportCourseId(Long importCourseId) {
        QueryWrapper<POCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("import_course_id", importCourseId);
        queryWrapper.eq("import_flag", 2);
        return poCourseDao.selectOne(queryWrapper);
    }

   /* @Override
    public Result updateCourseStateById(Integer id, String courseState) {
        poCourseDao.updateCourseStateById(id,courseState);
        return Result.success();
    }*/

    @Override
    public Result selectPoCourseStateById(Integer id) {
        String courseState = poCourseDao.selectPoCourseStateById(id);
        return Result.success(courseState);
    }
}
